//	ID : pallab81
// 	PROG : 469
// 	LANG : C++
// 	2011-07-06-21.17.54 Wednesday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define fo(i,x) for(int i = 0; i < x ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define LN(X) (int)(X).length()
#define SZ(X) (int)(X).size()
#define pb push_back
#define mk make_pair
#define f first
#define s second

int mov[8][2]={ {-1,0},{+1,0},{0,-1},{0,+1},{-1,-1},{-1,+1},{+1,+1},{+1,-1} };
vector< string > grid;
int vis[ 200 ][ 200 ];
inline int toI(string &ss){
    int d ;
    sscanf(ss.c_str(),"%d",&d);
return d;
}
inline pair<int,int> split(string &ss){
    stringstream st(ss);
    vector< string > vt;
    for(string tmp=""; getline(st,tmp,' '); ){
        if(LN(tmp)<1)continue;
        vt.pb( tmp );
    }
    pair< int ,int > ret = mk( toI(vt[0]),toI(vt[1]) );
return ret;
}
inline int dfs(int r, int c){
    memset(vis,-1,sizeof(vis));
    stack< pair<int,int> > st;
    if( grid[r][c]!='W')return 0;
    int d=1;
    vis[r][c]=0;
    st.push( mk(r,c) );

    while( st.empty() == false ){
        pair< int, int > peak = st.top();
        st.pop();
            fo(i,8){
                int tr = peak.f+mov[i][0];
                int tc = peak.s+mov[i][1];
                if(tr<0||tc<0||tr>=SZ(grid)||tc>=LN(grid[0]))continue;
                if( grid[tr][tc]=='W' && vis[tr][tc]!=0 ){
                    ++d;
                    vis[tr][tc]=0;
                    st.push( mk(tr,tc) );
                }
            }
    }


return d;
}
inline void doit(){
    string line;
    grid.clear();

    while(true){
        getline(cin,line);
        if( LN(line)<1 )break;
        else if(line[0]>='0' && line[0]<='9'){
            pair< int , int > num = split(line);
            printf("%d\n",dfs(num.f-1,num.s-1));
        }
        else{
            grid.pb(line);
        }
    }
}
int main(){

    string line;
    getline(cin,line);
    int cases = toI( line );
    getline(cin,line);
    string blank="";
    while(cases--){
        printf("%s",blank.c_str());
        doit();
        blank="\n";
    }


return 0;
}

